package org.boot.fast.model;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import io.swagger.v3.oas.annotations.Operation;
import org.boot.fast.util.FastQueryWrapperBuilder;
import org.boot.fast.pojo.CommonResult;
import org.boot.fast.pojo.PageResult;
import org.boot.fast.util.MappingUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

import static org.boot.fast.pojo.CommonResult.success;


@SuppressWarnings("unchecked")
public class FastController<T, S extends IService<T>> {
    private S service;
    private Class<T> entityClass;
    private Class<S> serviceClass;
    private FastQueryWrapperBuilder<T> builder;
    @Resource
    private ApplicationContext applicationContext;

    @PostConstruct
    public void init() {
        Type type = getClass().getGenericSuperclass();
        if (type instanceof ParameterizedType) {
            ParameterizedType parameterizedType = (ParameterizedType) type;
            entityClass = (Class<T>) parameterizedType.getActualTypeArguments()[0];
            serviceClass = (Class<S>) parameterizedType.getActualTypeArguments()[1];
            builder = FastQueryWrapperBuilder.of(entityClass);
            service = (S) applicationContext.getBean(serviceClass);
        }
    }


    @PostMapping("/insert")
    @Operation(summary = "新增数据")
    public CommonResult<Boolean> insert(@RequestBody T entity) {
        service.save(entity);
        return success(true);
    }

    @PostMapping("/insertBatch")
    @Operation(summary = "新增批量数据")
    public CommonResult<Boolean> insertBatch(@RequestBody List<T> entityList) {
        service.saveBatch(entityList);
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新数据")
    public CommonResult<Boolean> update(@RequestBody T entity) {
        return success(service.updateById(entity));
    }

    @PutMapping("/updateBatch")
    @Operation(summary = "更新批量数据")
    public CommonResult<Boolean> updateBatch(@RequestBody List<T> entityList) {
        return success(service.updateBatchById(entityList));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除数据")
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        return success(service.removeById(id));
    }

    @DeleteMapping("/deleteBatch")
    @Operation(summary = "删除批量数据")
    public CommonResult<Boolean> deleteBatch(@RequestBody List<Long> ids) {
        return success(service.removeByIds(ids));
    }

    @GetMapping("/get")
    @Operation(summary = "查询数据")
    public CommonResult<T> get(@RequestParam("id") Long id) {
        return success(service.getById(id));
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询")
    public CommonResult<PageResult<T>> page(@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                                            @RequestParam Map<String, Object> params
    ) {
        Map<String, Object> queryParams = new java.util.HashMap<>(params);
        queryParams.remove("pageNo");
        queryParams.remove("pageSize");
        return success(new PageResult<>(service.page(new Page<>(pageNo, pageSize), builder.buildQueryWrapper(queryParams))));
    }

    @GetMapping("/list")
    @Operation(summary = "列表查询")
    public CommonResult<List<T>> list(@RequestParam Map<String, Object> params) {
        return success(service.list(builder.buildQueryWrapper(params)));
    }




    /**
     * 获取所有可用字段名（用于调试）
     */
//    @GetMapping("/fields")
//    @Operation(summary = "获取所有可用字段")
//    public CommonResult<Map<String, Object>> getAvailableFields() {
//        return success(java.util.Collections.singletonMap("fields", builder.getFieldNames()));
//    }

}
